package ca.etsmtl.log730.lab3.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/*
 * JE SUIS LA BANQUE CENTRALE
 */
public class CentralBank implements IGotMoney, Serializable {

	private Map<String, Branch> branchMap = new HashMap<String, Branch>();
	
	/**
	 * yeah
	 */
	private static final long serialVersionUID = -3888249586130665052L;
	
	@Override
	public Float getAmount() {
		Map<String, Branch> temp = Collections.synchronizedMap(branchMap);
		Collection<Branch> branchs = temp.values();
		
		float total = 0;
		synchronized(temp) {
			Iterator<Branch> i = branchs.iterator(); // Must be in synchronized block
			while (i.hasNext())
				total+=i.next().getAmount();
		}
		return total;
	}

	/*
	 * retourne la succursale en fonction de son nom
	 */
	public Branch getBranch(String name) {
		return branchMap.get(name);
	}
	
	public void addBranch(Branch b) {
		branchMap.put(b.getName(), b);
	}
	
	public void removeBranch(String name) {
		branchMap.remove(name);
	}
	
	public List<Branch> getBranches() {
		return new ArrayList<Branch>(branchMap.values());
	}
	
	public void setBranches(List<Branch> bl) {
		
	}
}
